new Vue({
    el: '#main',
    data: () => ({
        type:'detail',
        details: {},
        linkarr: [],
        bank: [],
        linkpage: 1,
        bankpage:1,
        pageSize:2,
    }),
    mounted(){
        if(this.getQueryString('tab')=='link'){
            $('.nav-tabs .active').removeClass("active")
            $('.nav-tabs li').eq(1).addClass("active")
            $('.tab-content .active').removeClass("active")
            $('#table-linkman').addClass("active")
            this.getdata('linkman','linkpage')
        }else if(this.getQueryString('tab')=='bank'){
            $('.nav-tabs .active').removeClass("active")
            $('.nav-tabs li').eq(2).addClass("active")
            $('.tab-content .active').removeClass("active")
            $('#table-bank').addClass("active")
            this.getdata('bank','bankpage')
        }else{
            this.getdata('detail')
        }
        $('#linkman_Modal').on('hidden.bs.modal', (e)=>{
            this.getdata('linkman','linkpage')
          })
        $('#bank_Modal').on('hidden.bs.modal', (e)=>{
            this.getdata('bank','bankpage')
          })
    },
    methods: {
        getdata(type,page){
            if(type)
            this.type=type
            $.ajax({
                url: '/admin/customer/companycheck',
                data: {
                    company_id: $('#company_id').val(),//公司id
                    status: this.type,
                    page: this[page],
                    pageSize: this.pageSize
                },
                type: 'post',
                success: (data) => {
                    data = JSON.parse(data)
                    if (type == 'detail') {
                        //详细资料
                        this.details = data.data
                    } else if (type == 'linkman') {
                        //联系人
                        this.linkarr = data.data
                        if(data.size/this.linkpage>=1){
                            $('#pagination_link').show()
                            this.setPage_link(this.linkpage,data.size)
                        }else{
                            $('#pagination_link').hide()
                        }
                    } else if (type == 'bank') {
                        //银行
                        this.bank = data.data
                        if(data.size/this.bankpage>=1){
                            $('#pagination_link').show()
                            this.setPage_bank(this.bankpage, data.size)
                        }else{
                            $('#pagination_link').hide()
                        }
                    }
                }
            })
        },
        setPage_link(pageCurrent, pageSum) {
                //数据总条数
                var myPageCount = pageSum;
                //一页显示多少条
                var myPageSize = this.pageSize;
                //总页数
                var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
                $.jqPaginator('#pagination_link', {
                    totalPages: parseInt(countindex),
                    //最多显示多少页
                    visiblePages: 5,
                    //默认显示第几页
                    currentPage: pageCurrent,
                    first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                    prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                    next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                    last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                    page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                    all:'<li class="page"><a href="javascript:;">'+parseInt(countindex)+' / '+this.linkpage+'PG , '+this.pageSize+' /PG</a></li>',
                    onPageChange: (num, type) => {
                        if (type == "change") {
                            this.linkpage = num
                            this.getdata('linkman','linkpage')
                        }
                    }
                });
            },
        setPage_bank(pageCurrent, pageSum) {
                //数据总条数
                var myPageCount = pageSum;
                //一页显示多少条
                var myPageSize = this.pageSize
                //总页数
                var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
                $.jqPaginator('#pagination_bank', {
                    totalPages: parseInt(countindex),
                    //最多显示多少页
                    visiblePages: 5,
                    //默认显示第几页
                    currentPage: pageCurrent,
                    first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                    prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                    next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                    last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                    page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                    all:'<li class="page"><a href="javascript:;">'+parseInt(countindex)+' / '+this.bankpage+'PG , '+this.pageSize+' /PG</a></li>',
                    onPageChange: (num, type) => {
                        if (type == "change") {
                            this.bankpage = num
                            this.getdata('bank','bankpage')
                        }
                    }
                });
            },
        //联系人的删除
        dellink(id,index){
            $.ajax({
                url: '/admin/customer/liaisondel?linkid='+id,
                type: 'post',
                success: (data) => {
                    data = JSON.parse(data)
                    change_mes_type('.alert',data.status)
                    if (data.status != 1) {
                        $('.alert').show()
                        $('.alert p').html('*' + data.msg + '<br>')
                    } else {
                        $('.alert').show()
                        $('.alert p').html('*' + data.msg + '<br>')
                        if(this.linkpage > Math.ceil(this.linkarr.length-1 / this.pageSize)){
                            if(this.linkpage>1){
                                this.linkpage--
                            }
                        }
                        this.getdata('linkman','linkpage')
                    }

                }
            })
        },
        //银行的删除
        delbank(id,index){
            $.ajax({
                url: '/admin/customer/bankdel?bankid='+id,
                type: 'post',
                success: (data) => {
                    data = JSON.parse(data)
                    change_mes_type('.alert',data.status)
                    if (data.status != 1) {
                        //toastr.warning(res.msg);
                        $('.alert').show()
                        $('.alert p').html('*' + data.msg + '<br>')
                    } else {
                        //this.bank.splice(index,1)
                        $('.alert').show()
                        $('.alert p').html('*' + data.msg + '<br>')
                        if(this.bankpage > Math.ceil(this.bank.length-1 / this.pageSize)){
                            if(this.bankpage>1){
                                this.bankpage--
                            }
                        }
                        this.getdata('bank','bankpage')
                    }

                }
            })
        },
        returnlist(){
            $.ajax({
                url: '/admin/api/isbanklink?status',
                type: 'post',
                success: (data) => {
                    if (data.code == 1) {
                        $('#breaklist').modal('show')
                    } else {
                        location.href="/admin/customer/customercompany"
                    }
                }
            })
        },
        getQueryString(name){
            var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if(r!=null)return  unescape(r[2]); return null;
       }
    },
    watch: {
        linkpage: function () {
            this.getdata('linkman','linkpage')
        },
        bankpage: function () {
            this.getdata('bank','bankpage')
        }
    }
})
$("#linkman_Modal").on("hidden.bs.modal", function () {
    $(this).removeData("bs.modal");
    $(".modal-content").children().remove();
});
$("#bank_Modal").on("hidden.bs.modal", function () {
    $(this).removeData("bs.modal");
    $(".modal-content").children().remove();
});
$('.savebtn').click(function(){
    $.ajax({
        url:'/admin/api/isbanklink?isexist',
        type:'post',
        success:function(data){
            location.href="/admin/customer/customercompany"
        }
    })
})